<template>
  <div class="navigation-bar">
    <Hamburger
      v-if="!isTop || isMobile"
      :is-active="appStore.sidebar.opened"
      class="hamburger"
      @toggle-click="toggleSidebar"
    />
    <Breadcrumb v-if="!isTop || isMobile" class="breadcrumb" />
    <Sidebar v-if="isTop && !isMobile" class="sidebar" />
    <div class="right-menu">
      <div v-if="showDownload" style="cursor: pointer; padding: 0 8px" @click="createApp">
        <SvgIcon name="xiazai" />
      </div>
      <LanguageSwitch class="right-menu-item" />
      <SearchMenu v-if="showSearchMenu" class="right-menu-item" />
      <Screenfull v-if="showScreenfull" class="right-menu-item" />
      <ThemeSwitch v-if="showThemeSwitch" class="right-menu-item" />
      <Notify v-if="showNotify" class="right-menu-item" />
      <el-dropdown class="right-menu-item" trigger="hover">
        <div class="right-menu-avatar">
          <el-avatar :icon="UserFilled" :size="30" />
          <span>{{ userStore.username }}</span>
          <el-icon :size="14"><CaretBottom /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <router-link to="/">
              <el-dropdown-item>{{ $t('首页') }}</el-dropdown-item>
            </router-link>
            <el-dropdown-item divided @click="logout">
              {{ $t('退出登录') }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <div class="setting-menu" @click="openSetting = true">
        <el-icon :size="18"><Setting /></el-icon>
      </div>
    </div>
    <el-drawer v-model="openSetting" append-to-body size="300px" :with-header="false">
      <Settings />
    </el-drawer>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { storeToRefs } from 'pinia'
import { useUserStore, useSettingsStore, useAppStore } from '@/store'
import { UserFilled } from '@element-plus/icons-vue'
import Hamburger from '../Hamburger/index.vue'
import Breadcrumb from '../Breadcrumb/index.vue'
import Sidebar from '../Sidebar/index.vue'
import Notify from '@/components/Notify/index.vue'
import LanguageSwitch from '../LanguageSwitch/index.vue'
import ThemeSwitch from '@/components/ThemeSwitch/index.vue'
import Screenfull from '@/components/Screenfull/index.vue'
import SearchMenu from '@/components/SearchMenu/index.vue'
import { useDevice } from '@/hooks/useDevice'
import { useLayoutMode } from '@/hooks/useLayoutMode'
import Settings from '../Settings/index.vue'
import { $t } from '@/languages/index'

const { isMobile } = useDevice()
const { isTop } = useLayoutMode()
const router = useRouter()
const appStore = useAppStore()
const userStore = useUserStore()
const settingsStore = useSettingsStore()
const { showNotify, showThemeSwitch, showScreenfull, showSearchMenu, showDownload } = storeToRefs(settingsStore)

const openSetting = ref(false)
/** 切换侧边栏 */
const toggleSidebar = () => {
  appStore.toggleSidebar(false)
}

/** 登出 */
const logout = () => {
  userStore.logout()
  router.push('/login')
}

/**安装pwa */
const createApp = async () => {
  if (localStorage.getItem('addDesktop')) {
    proxy.$message.warning($t('桌面应用已创建'))
    return
  }
  if (!window.deferredPrompt) {
    proxy.$message.error($t('当前环境不支持安装桌面应用'))
    return
  }
  try {
    await window.deferredPrompt.prompt()
    const result = await window.deferredPrompt.userChoice
    if (result.outcome === 'accepted') {
      localStorage.setItem('addDesktop', 'true')
    } else {
      console.log('用户取消安装桌面应用')
    }
  } catch (error) {
    console.error('PWA installation error:', error)
    proxy.$message.error($t('安装过程出现错误'))
  } finally {
    // 清理提示对象
    window.deferredPrompt = null
  }
}
</script>

<style lang="scss" scoped>
.navigation-bar {
  height: var(--v3-navigationbar-height);
  overflow: hidden;
  color: var(--v3-navigationbar-text-color);
  display: flex;
  justify-content: space-between;
  flex: 1;
  .hamburger {
    display: flex;
    align-items: center;
    height: 100%;
    padding: 0 15px;
    cursor: pointer;
  }
  .breadcrumb {
    flex: 1;
    // 参考 Bootstrap 的响应式设计将宽度设置为 576
    @media screen and (max-width: 576px) {
      display: none;
    }
  }
  .sidebar {
    flex: 1;
    // 设置 min-width 是为了让 Sidebar 里的 el-menu 宽度自适应
    min-width: 0px;
    :deep(.el-menu) {
      background-color: transparent;
    }
    :deep(.el-sub-menu) {
      &.is-active {
        .el-sub-menu__title {
          color: var(--el-color-primary) !important;
        }
      }
    }
  }
  .right-menu {
    margin-right: 5px;
    height: 100%;
    display: flex;
    align-items: center;
    .right-menu-item {
      padding: 0 10px;
      cursor: pointer;
      .right-menu-avatar {
        display: flex;
        align-items: center;
        .el-avatar {
          margin-right: 5px;
        }
        span {
          font-size: 16px;
        }
      }
    }
    .setting-menu {
      cursor: pointer;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      padding: 0 6px;
      transition: transform 0.3s ease;
      &:hover .el-icon {
        transform: rotate(360deg);
      }
    }
    .el-icon {
      transition: transform 0.3s ease;
    }
  }
}
</style>
